package cuit.epoch.pymjl.security.controller;


import cuit.epoch.pymjl.security.common.result.CommonResult;
import cuit.epoch.pymjl.security.common.result.ResultUtils;
import cuit.epoch.pymjl.security.entity.dto.UserDTO;
import cuit.epoch.pymjl.security.service.IUserService;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Pymjl
 * @since 2022-08-20
 */
@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Autowired
    IUserService userService;

    @PostMapping("/register")
    public CommonResult<String> registry(@Valid @RequestBody UserDTO userDTO) {
        userService.registry(userDTO);
        return ResultUtils.success();
    }

    @PostMapping("/login")
    public CommonResult<String> login(@NotBlank(message = "用户名不能为空") @RequestParam String username,
                                      @Length(min = 6, max = 255, message = "密码长度不能小于6位") @RequestParam String password) {
        return ResultUtils.success(userService.login(username, password));
    }

    @GetMapping("/test")
    @PreAuthorize("hasAuthority('admin:test')")
    public CommonResult<String> test() {
        return ResultUtils.success("test");
    }
}

